#cmake file for project facedetect
#author:guyadong
#created:2016/01/09
############################################################################
cmake_minimum_required( VERSION 2.6 )

##将全局属性INCLUDE_OPENCL_REQUIRED指定的路径加入include路径列表中
if( INCLUDE_OPENCL_REQUIRED )
	include_directories(${INCLUDE_OPENCL_REQUIRED})
else()
	message(FATAL_ERROR "not found include directory for OpenCL " )
endif()
include_directories(
	${INCLUDE_CIMG} 
	${LIBJPEG_TURBO_DIR}/include 
	${OPENJPEG_DIR}/include
	${COMMONS_SOURCE_FOLDER} 
	${PROJECT_SOURCE_DIR}/include
	${PROJECT_SOURCE_DIR}/jsoncpp  
	${PROJECT_SOURCE_DIR}/faceimage/cimgwrapper
	${INCLUDE_B64}
	)

MESSAGE(STATUS "opencl include directory :${INCLUDE_OPENCL_REQUIRED}")
############################################################################
## 加入__CL_ENABLE_EXCEPTIONS宏定义，使所有OpenCL调用出错时抛出异常
## 加入DETECT_CL宏定义,用于内部编译控制
add_definitions(-D__CL_ENABLE_EXCEPTIONS)
message(STATUS "add #define __CL_ENABLE_EXCEPTIONS" )
add_executable( test_cl EXCLUDE_FROM_ALL test_cl.cpp cl_utilits.cpp gray_matrix_cl.cpp facecl_context.cpp tls_var.cpp display_tool.cpp)
target_link_libraries(test_cl img_tool ${OpenCL_LIBRARY})
add_executable( test_casinfo EXCLUDE_FROM_ALL test_casinfo.cpp casinfo.cpp measure.cpp)
add_executable( cas2json EXCLUDE_FROM_ALL cas2json.cpp casinfo.cpp)
add_executable( data2source EXCLUDE_FROM_ALL data2source.cpp data2source_main.cpp)
add_executable( test_detect test_detect.cpp)
add_executable( test_fd_old EXCLUDE_FROM_ALL test_fd_old.cpp adaboost_facedetect_old.cpp)
if(ZLIB_LIBRARY_STATIC)
set (z_static ${ZLIB_LIBRARY_STATIC})
include_directories(	${INCLUDE_ZLIB})
else()
# 使用gcc编译器自带的zlib库
set (z_static z)
endif()
target_link_libraries(test_casinfo jsoncpp ${z_static} b64 ${OpenCL_LIBRARY})
target_link_libraries(cas2json jsoncpp ${z_static} b64 ${OpenCL_LIBRARY})
target_link_libraries(data2source  ${z_static})
add_library(detect_cl SHARED detectface.cpp casinfo.cpp  cl_utilits.cpp gray_matrix_cl.cpp facecl_context.cpp tls_var.cpp detect_param.cpp measure.cpp casinfo_code.cpp file_from_code.cpp display_tool.cpp)
add_library(img_tool SHARED img_tool.cpp)
#######Windows 下动态库和导入库(import library)文件名前缀为空 #######
if(CMAKE_SYSTEM_NAME MATCHES "Windows")
set_property(TARGET  detect_cl img_tool PROPERTY PREFIX "")
set_property(TARGET  detect_cl img_tool PROPERTY IMPORT_PREFIX "")
endif(CMAKE_SYSTEM_NAME MATCHES "Windows")
if(CMAKE_COMPILER_IS_GNUCXX)    
if(WIN32)
	#windows下将pthread库静态链接
	target_link_libraries(detect_cl -static -pthread)
	target_link_libraries(img_tool -static -pthread)
	#WIN32下静态链接c标准库
	set_property(TARGET  detect_cl img_tool test_cl test_casinfo cas2json data2source
						APPEND_STRING 
						PROPERTY LINK_FLAGS " -static-libgcc ")
endif(WIN32)
#静态链接c++标准库					
set_property(TARGET  detect_cl img_tool test_cl test_casinfo cas2json data2source
					APPEND_STRING 
					PROPERTY LINK_FLAGS " -static-libstdc++ ")
# -Wl,--enable-stdcall-fixup 用于关闭连接OpenCL动态库时的警告,用不到OpenCL库的不需要添加
set_property(TARGET  detect_cl test_cl test_casinfo cas2json
					APPEND_STRING 
					PROPERTY LINK_FLAGS " -Wl,--enable-stdcall-fixup ")
endif(CMAKE_COMPILER_IS_GNUCXX)
# GF_DLL_BUILD 控制detect_cl.h文件中动态库接口函数的定义
# CL_FILES_EMBEDDED 控制将opencl代码编译到程序中
# CASINFO_EMBEDDED控制将分类器数据编译到程序中
set_target_properties(detect_cl PROPERTIES COMPILE_FLAGS   "-DGF_DLL_BUILD -DCASINFO_EMBEDDED -DCL_FILES_EMBEDDED")
# 去掉CL_FILES_EMBEDDED定义用于kernel代码调试
#set_target_properties(detect_cl PROPERTIES COMPILE_FLAGS   "-DGF_DLL_BUILD -DCASINFO_EMBEDDED")
target_link_libraries(detect_cl faceimage ${JPEG_LIBRARY_STATIC} ${OPENJP2_LIBRARY_STATIC}  jsoncpp ${z_static} b64 ${OpenCL_LIBRARY})
set_target_properties(img_tool PROPERTIES COMPILE_FLAGS   -DGF_DLL_BUILD)
target_link_libraries(img_tool  faceimage ${JPEG_LIBRARY_STATIC} ${OPENJP2_LIBRARY_STATIC} )


target_link_libraries(test_fd_old faceimage ${JPEG_LIBRARY_STATIC} ${OPENJP2_LIBRARY_STATIC})
target_link_libraries(test_detect  detect_cl img_tool)

########安装脚本##########
install(TARGETS detect_cl img_tool test_detect 
        RUNTIME DESTINATION bin
        LIBRARY DESTINATION lib
        ARCHIVE DESTINATION lib)
install(FILES ${PROJECT_SOURCE_DIR}/facedetect/README_utf8.txt DESTINATION .)        
install(FILES ${PROJECT_SOURCE_DIR}/facedetect/detect_cl_types.h DESTINATION include)
install(FILES ${PROJECT_SOURCE_DIR}/facedetect/detect_cl.h DESTINATION include)
install(FILES ${PROJECT_SOURCE_DIR}/facedetect/img_tool.h DESTINATION include)
install(FILES ${PROJECT_SOURCE_DIR}/facedetect/CMakeLists.txt.test_detect.in DESTINATION sample RENAME CMakeLists.txt)
install(FILES ${PROJECT_SOURCE_DIR}/facedetect/test_detect.cpp DESTINATION sample)
install(FILES ${COMMONS_SOURCE_FOLDER}/raii.h DESTINATION sample)
install(FILES ${COMMONS_SOURCE_FOLDER}/assert_macros.h DESTINATION sample)
install(FILES ${COMMONS_SOURCE_FOLDER}/cmdline.h DESTINATION sample)
install(FILES ${COMMONS_SOURCE_FOLDER}/dirent.h DESTINATION sample)
install(FILES ${COMMONS_SOURCE_FOLDER}/file_utilits.h DESTINATION sample)
install(FILES ${COMMONS_SOURCE_FOLDER}/time_utilits.h DESTINATION sample)